package cn.hs.ph.servlet;

import cn.hs.ph.dao.CourierStationDao;
import cn.hs.ph.dao.impl.CourierStationDaoImpl;

import cn.hs.ph.entity.CourierStation;

import cn.hs.ph.entity.Shop;
import cn.hs.ph.util.Result;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/courierstation")
public class CourierStationServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.设置编码方式
        //2.读取action，并判断
        String action=req.getParameter("action");
        CourierStationDao courierStationDao=new CourierStationDaoImpl();
        //3.根据action执行不同的代码
        if ("selectAll".equals(action)){
            //3.1接收数据
            String pageNum=req.getParameter("pageNum");
            String pageSize=req.getParameter("pageSize");
            String startTime=req.getParameter("startTime");
            String endTime=req.getParameter("endTime");
            String courierStationName=req.getParameter("courierStationName");
            String communityName=req.getParameter("communityName");
            //3.2 封装到map集合
            Map<String,String> map=new HashMap();
            map.put("pageNum",pageNum);
            map.put("pageSize",pageSize);
            map.put("startTime",startTime);
            map.put("endTime",endTime);
            map.put("communityName",communityName);
            map.put("courierStationName",courierStationName);
            //3.3 连接数据库操作
            //3.3.1 查询所有的数据
            List<CourierStation> list=new CourierStationDaoImpl().selectAll(map);
            //3.3.2 查询总条数
            int total=new CourierStationDaoImpl().selectCount(map);
            //3.4 给出响应信息
            Map resultMap=new HashMap();
            resultMap.put("list",list);
            resultMap.put("total",total);
            Result result = Result.success(resultMap);
            resp.getWriter().write(JSON.toJSONString(result));
        } else if("add".equals(action)){  //新增和修改
            //收集数据
            String id = req.getParameter("courierStationCommunityId");
            int courierStationCommunityId = 0;
            if (id!=null && !"".equals(id)){
                courierStationCommunityId=Integer.parseInt(id);
            }
            String courierStationProvince = req.getParameter("courierStationProvince");
            String courierStationCity = req.getParameter("courierStationCity");
            String courierStationDistrict = req.getParameter("courierStationDistrict");
            String courierStationName = req.getParameter("courierStationName");
            String courierStationAddress = req.getParameter("courierStationAddress");
            String status = req.getParameter("courierStationStatus");
            int courierStationStatus=1;
            if (status!=null && !"".equals(status)){
                courierStationStatus=Integer.parseInt(status);
            }
            String courierStationCommunityName=req.getParameter("courierStationCommunityName");
            //组成对象
            CourierStation courierStation=new CourierStation(courierStationName,courierStationProvince,courierStationCity,courierStationDistrict,courierStationCommunityId,courierStationAddress,courierStationStatus,courierStationCommunityName);
            //新增
            int num = courierStationDao.insert(courierStation);
                Result result=null;
                if (num!=0){  //新增成功
                    result = Result.success("新增成功");
                }else {
                    result = Result.error("新增失败");
                }
                resp.getWriter().write(JSON.toJSONString(result));
        }else if ("update".equals(action)){
            //收集数据
            String id = req.getParameter("courierStationId");
            int courierStationId = 0;
            if (id!=null && !"".equals(id)){
                courierStationId=Integer.parseInt(id);
            }
            String status=req.getParameter("courierStationStatus");
            int courierStationStatus=1;
            if (status!=null && !"".equals(status)){
                courierStationStatus=Integer.parseInt(status);
            }
            //组成对象
            CourierStation courierStation=new CourierStation(courierStationId,courierStationStatus);
            //修改
            int num = courierStationDao.update(courierStation);
            Result result=null;
            if (num!=0){  //新增成功
                result = Result.success("修改成功");
            }else {
                result = Result.error("修改失败");
            }
            resp.getWriter().write(JSON.toJSONString(result));
        } else if ("delete".equals(action)){
            //收集数据
            String id = req.getParameter("courierStationId");
            int courierStationId = 0;
            if (id!=null && !"".equals(id)){
                courierStationId=Integer.parseInt(id);
            }
            int num = courierStationDao.delete(courierStationId);
            Result result=null;
            if (num!=0){  //删除
                result = Result.success("删除成功");
            }else {
                result = Result.error("删除失败");
            }
            resp.getWriter().write(JSON.toJSONString(result));
        }
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
